package m5;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

/*
    2022/1/20
    16:35
*/public class Producer {
    public static void main(String[] args) throws Exception{
        //连接服务器
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.64.140");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);

        while (true) {
            System.out.println("请输入消息:");
            String s = new Scanner(System.in).nextLine();
            System.out.println("请输入路由键:");
            String k = new Scanner(System.in).nextLine();
            //对默认交换机"",会自动使用队列名作为路由键
            channel.basicPublish("topic_logs",k,null,s.getBytes());
        }
    }
}
